package utils

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/BurntSushi/toml"
	"regexp"
	"strconv"
	"time"
)

func ReadToml(file_path string, dst_obj interface{}) {
	if _, err := toml.DecodeFile(file_path, dst_obj); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(dst_obj)
	}
}

// 验证手机号
func VerifyMobileFormat(cell int64) bool {

	strPhone := strconv.FormatInt(cell, 10)
	regular := "^[0-9]{11}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(strPhone)
}

func VerifyMobileFormatAny(cell interface{}) bool {
	if cell == nil {
		return false
	}
	if val, ok := cell.(int64); ok {
		return VerifyMobileFormat(val)
	}
	if val, ok := cell.(string); ok {
		v, _ := strconv.ParseInt(val, 10, 64)
		return VerifyMobileFormat(v)
	}
	return false
}

func GetDateGMT() string {
	timeFormat := "Mon, 02 Jan 2006 15:04:05 GMT"
	zone, _ := time.LoadLocation("GMT")
	timeStr := time.Now().In(zone).Format(timeFormat)
	fmt.Printf("\n\n getDateGMT [%v]", timeStr)
	return timeStr
}

func GetDateUTC() string {
	timeFormat := "2006-01-02T15:04:05Z"
	zone, _ := time.LoadLocation("UTC")
	timeStr := time.Now().In(zone).Format(timeFormat)
	fmt.Printf("\n\n getDateGMT [%v]", timeStr)
	return timeStr
}

func HmacSHA1UTF8(value string, secret string) string {
	h := hmac.New(sha1.New, []byte(secret))
	h.Write([]byte(value))
	hmacSign := base64.StdEncoding.EncodeToString(h.Sum(nil))

	return hmacSign
}

// 结构体转map
func StructToMap(struct_val interface{}) map[string]interface{} {
	b, _ := json.Marshal(struct_val)
	m := make(map[string]interface{})
	_ = json.Unmarshal(b, m)
	return m
}
